package com.example.companybusiness.operationc;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.companybusiness.MainActivity;
import com.example.companybusiness.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


public class StarbrandFragment extends Fragment {

    private static final String TAG = MainActivity.class.getSimpleName();
    private List<StartBrandAdapterBean> dataList;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    public StarbrandFragment() {
    }

    public static StarbrandFragment newInstance(String param1, String param2) {
        StarbrandFragment fragment = new StarbrandFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_starbrand, container, false);
        iniView(inflate);
        refresh(inflate);
        return inflate;
    }

    private void refresh(View inflate) {
        SmartRefreshLayout smartRefresh = inflate.findViewById(R.id.star_brand_smart_refresh);
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull  RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1000,true,false);
            }
        });
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull  RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1000,true,false);
            }
        });
    }

    private void iniView(View inflate) {
        RecyclerView recyclerView=inflate.findViewById(R.id.star_brand_recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        dataList=new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            StartBrandAdapterBean sbab = new StartBrandAdapterBean();
            sbab.setIcon(R.mipmap.ic_launcher);
            sbab.setName("阿里"+i);
            sbab.setCompany("有限公司"+i);
            dataList.add(sbab);
            //Log.d(TAG, "onViewCreated: "+dataList.get(i).getName());
        }
        StarBrandAdapter adapter = new StarBrandAdapter(requireContext());
        adapter.setDataList(dataList);
        recyclerView.setAdapter(adapter);

        adapter.setmListener(new StarBrandAdapter.onStarItemClick() {
            @Override
            public void onStarclick(int position) {
                Toast.makeText(getActivity(), "点击了"+dataList.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }


}